using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using FaceCat;
using System;

namespace facecat_avalonia.Views;

/// <summary>
/// 自定义视图
/// </summary>
public partial class MainView : UserControl
{
    /// <summary>
    /// 构造函数
    /// </summary>
    public MainView()
    {
        InitializeComponent();
        onLoad();
    }

    /// <summary>
    /// 是否双击
    /// </summary>
    private bool m_isDoubleClick;

    /// <summary>
    /// 公共方法库
    /// </summary>
    public FCNative m_native;

    /// <summary>
    /// 设备对接
    /// </summary>
    public AvaloniaHost m_host;

    /// <summary>
    /// 配置解析
    /// </summary>
    public FCUIXml m_xml;

    /// <summary>
    /// 鼠标滚轮事件
    /// </summary>
    /// <param name="sender">调用者</param>
    /// <param name="e">参数</param>
    private void MainView_PointerWheelChanged(object? sender, Avalonia.Input.PointerWheelEventArgs e)
    {
        if (m_host.allowOperate())
        {
            FCTouchInfo newTouchInfo = new FCTouchInfo();
            newTouchInfo.m_delta = (int)e.Delta.Y;
            m_native.onMouseWheel(newTouchInfo);
        }
    }

    public bool m_rightClick;

    /// <summary>
    /// 鼠标松开事件
    /// </summary>
    /// <param name="sender">调用者</param>
    /// <param name="e">参数</param>
    private void MainView_PointerReleased(object? sender, Avalonia.Input.PointerReleasedEventArgs e)
    {
        m_host.m_touchPoint = new FCPoint((int)e.GetPosition(this).X, (int)e.GetPosition(this).Y);
        if (m_host.allowOperate())
        {
            FCTouchInfo newTouchInfo = new FCTouchInfo();
            m_rightClick = e.GetCurrentPoint(this).Properties.IsRightButtonPressed;
            if (m_rightClick)
            {
                newTouchInfo.m_secondTouch = true;
            }
            else
            {
                newTouchInfo.m_firstTouch = true;
            }
            newTouchInfo.m_buttons = 1;
            newTouchInfo.m_clicks = m_isDoubleClick ? 2 : 1;
            m_native.onMouseUp(newTouchInfo);
        }
    }

    /// <summary>
    /// 鼠标移动事件
    /// </summary>
    /// <param name="sender">调用者</param>
    /// <param name="e">参数</param>
    private void MainView_PointerMoved(object? sender, Avalonia.Input.PointerEventArgs e)
    {
        m_host.m_touchPoint = new FCPoint((int)e.GetPosition(this).X, (int)e.GetPosition(this).Y);
        if (m_host.allowOperate())
        {
            if (m_native.m_touchDownView != null)
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
                newTouchInfo.m_clicks = 1;
                m_native.onMouseMove(newTouchInfo);
            }
            else
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                m_native.onMouseMove(newTouchInfo);
            }
        }
    }

    /// <summary>
    /// 鼠标按下事件
    /// </summary>
    /// <param name="sender">调用者</param>
    /// <param name="e">参数</param>
    private void MainView_PointerPressed(object? sender, Avalonia.Input.PointerPressedEventArgs e)
    {
        m_host.m_touchPoint = new FCPoint((int)e.GetPosition(this).X, (int)e.GetPosition(this).Y);
        if (m_host.allowOperate())
        {
            FCTouchInfo newTouchInfo = new FCTouchInfo();
            m_rightClick = e.GetCurrentPoint(this).Properties.IsRightButtonPressed;
            if (m_rightClick)
            {
                newTouchInfo.m_secondTouch = true;
            }
            else
            {
                newTouchInfo.m_firstTouch = true;
            }
            newTouchInfo.m_buttons = 1;
            newTouchInfo.m_clicks = e.ClickCount;
            m_isDoubleClick = e.ClickCount > 1;
            m_native.onMouseDown(newTouchInfo);
        }
    }

    /// <summary>
    /// 绘图方法
    /// </summary>
    /// <param name="context">上下文</param>
    public override void Render(DrawingContext context)
    {
        base.Render(context);
        int width = (int)Width, height = (int)Height;
        FCSize oldSize = m_native.getSize();
        if (width > 0 && height > 0)
        {
            if (oldSize.cx != width || oldSize.cy != height)
            {
                m_native.setSize(new FCSize(width, height));
                m_native.update();
            }
        }
        AvaloniaPaint aPaint = m_native.getPaint() as AvaloniaPaint;
        aPaint.m_context = context;
        FCRect clipRect = new FCRect(0, 0, m_native.getSize().cx, m_native.getSize().cy);
        aPaint.beginPaint(0, clipRect, clipRect);
        m_native.onPaint(clipRect);
        aPaint.endPaint();
    }

    /// <summary>
    /// 加载视图
    /// </summary>
    public void onLoad()
    {
        m_xml = new FCUIXml();
        m_xml.createNative();
        m_native = m_xml.getNative();
        m_native.setPaint(new AvaloniaPaint());
        m_host = new AvaloniaHost();
        m_host.setView(this);
        m_native.setHost(m_host);
        m_host.setNative(m_native);
        m_native.setSize(new FCSize(800, 700));
        string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
  <html xmlns=""facecat"">
    <body>
      <div dock=""fill"" backcolor=""rgb(140,192,81)"">
        <div name=""Div"" text=""Div"" location=""368,127"" size=""247,326"" align=""center"" vertical-align=""middle"" backcolor=""rgb(170,178,189)"" cornerradius=""10"">
          <input type=""text"" name=""TextBox"" location=""14,18"" size=""220,45"" cornerradius=""0"" bordercolor=""rgb(0,0,0)"" backcolor=""rgb(42,48,72)"" font=""Default,30.0"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnAC"" text=""AC"" location=""14,69"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(62,72,95)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnAddSub"" text=""+/-"" location=""69,69"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(62,72,95)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnMod"" text=""%"" location=""124,69"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(62,72,95)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnDivide"" text=""÷"" location=""179,69"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum7"" text=""7"" location=""14,117"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum8"" text=""8"" location=""69,117"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum9"" text=""9"" location=""124,117"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnMulti"" text=""×"" location=""179,117"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum4"" text=""4"" location=""14,165"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum5"" text=""5"" location=""69,165"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum6"" text=""6"" location=""124,165"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnSub"" text=""-"" location=""179,165"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum1"" text=""1"" location=""14,213"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum2"" text=""2"" location=""69,213"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum3"" text=""3"" location=""124,213"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnAdd"" text=""+"" location=""179,213"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum0"" text=""0"" location=""14,261"" size=""110,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnDot"" text=""."" location=""124,261"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnResult"" text=""="" location=""179,261"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
        </div>
      </div>
    </body>
  </html>";
        m_xml.loadXml(xml, null);
        m_native.update();
        this.PointerPressed += MainView_PointerPressed;
        this.PointerMoved += MainView_PointerMoved;
        this.PointerReleased += MainView_PointerReleased;
        this.PointerWheelChanged += MainView_PointerWheelChanged;
        m_native.update();
        m_native.invalidate();
    }
}
